<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        {include file="common/head" /}
    </head>

    <body>
    {include file="common/bread" /}
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5" action="" method="get">

                                <div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" id="brand_name" name="brand_name" value="{$gd.brand_name|default=''}" placeholder="品牌名称" autocomplete="off" class="layui-input">
                                </div>

                                <div class="layui-input-inline layui-show-xs-block">
                                    <select id="status" name="status" lay-filter="status">
                                        <option value="-2">选择状态</option>
                                        <option value="1">禁用</option>
                                        <option value="2">正常</option>
                                    </select>

                                    <script>$("#status option[value = '{$gd.status|default=-2}']").attr("selected","selected");</script>
                                </div>

                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn" type="submit">
                                        <i class="layui-icon">&#xe615;</i>
                                        搜索
                                    </button>
                                </div>
                            </form>
                        </div>

                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <!--<button class="layui-btn" onclick="delAll()"><i class="layui-icon">&#xe642;</i>批量标已读</button>-->
                        </div>
                        <div class="layui-card-body ">

                            <!--每个tr 上有两个属性 cate-id='1' 当前分类id fid='0' 父级id ,顶级分类为 0，有子分类的前面加收缩图标<i class="layui-icon x-show" status='true'>&#xe623;</i>-->
                            <table class="layui-table layui-form">
                              <thead>
                                <tr>
                                  <th width="20">
                                    <input type="checkbox" id="c_all" name="" lay-skin="primary" lay-filter="all">
                                  </th>
                                  <th>品牌首字母</th>
                                  <th>品牌</th>
                                  <th>品牌ID</th>
                                  <th>品牌logo</th>
                                  <th>热门</th>
                                  <th>首页推荐</th>
                                  <th>状态</th>
                                  <th width="250">操作</th>
                              </thead>
                              <tbody class="x-cate">
                              {volist name="list" id="vo"}
                              <tr  >
                                  <td>
                                      <input type="checkbox" class="ids" name="ids" value="{$vo.id}" lay-skin="primary" lay-filter="child">
                                  </td>
                                  <td>
                                      {$vo['brand_first']}
                                  </td>
                                  <td>
                                      {$vo['brand']}
                                  </td>
                                  <td>
                                      {$vo['brand_id']}
                                  </td>
                                  <td>
                                      <img src="{$vo['brand_logo']}" style="width: 50px; height: 50px;" alt="">
                                  </td>
                                  <td>
                                      <input type="checkbox" name="{$vo['id']}" value="{$vo['id']}" lay-text="热门|默认" {eq name="vo['shot']" value="1"}checked=""{/eq} lay-skin="switch" lay-filter="shot">
                                  </td>
                                  <td>
                                      <input type="checkbox" name="{$vo['id']}" value="{$vo['id']}" lay-text="推荐中|不推荐" {eq name="vo['hot']" value="1"}checked=""{/eq} lay-skin="switch" lay-filter="hot">
                                  </td>
                                  <td>
                                      <input type="checkbox" name="{$vo['id']}" value="{$vo['id']}" lay-text="正常|禁用" {eq name="vo['status']" value="1"}checked=""{/eq} lay-skin="switch" lay-filter="status">
                                  </td>
                                  <td class="td-manage">
                                      <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','{:url("edit",["id"=>$vo["id"],'model'=>'brand'])}')" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                      <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del(this,'{$vo["id"]}')" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                  </td>
                              </tr>

                              {/volist}

                              </tbody>
                            </table>
                        </div>
                        <div class="layui-card-body ">
                            {$list|raw}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
          layui.use(['form'], function(){
            form = layui.form;

              form.on('switch(status)', function(data){
                  $.ajax({
                      type: "POST",
                      url: "{:url('brand_status')}",
                      data: {id:data.value,status:data.elem.checked?1:0},
                      dataType:'json',
                      success: function (result) {
                          if(result.code){
                              layer.msg(result.msg,{icon:1,time:1000});
                          }else{
                              layer.msg(result.msg,{icon:2,time:1000});
                          }
                      },
                      error: function(data) {
                          layer.msg(data.msg,{icon:2,time:1000});
                      }

                  });
              });

              form.on('switch(hot)', function(data){
                  $.ajax({
                      type: "POST",
                      url: "{:url('brand_hot')}",
                      data: {id:data.value,hot:data.elem.checked?1:0},
                      dataType:'json',
                      success: function (result) {
                          console.log(result);
                          if(result.code){
                              layer.msg(result.msg,{icon:1,time:1000});
                          }else{
                              layer.msg(result.msg,{icon:2,time:1000});
                          }
                      },
                      error: function(data) {
                          layer.msg(data.msg,{icon:2,time:1000});
                      }

                  });
              });
              form.on('switch(shot)', function(data){
                  $.ajax({
                      type: "POST",
                      url: "{:url('brand_hot')}",
                      data: {id:data.value,hot:data.elem.checked?1:0,type:1},
                      dataType:'json',
                      success: function (result) {
                          console.log(result);
                          if(result.code){
                              layer.msg(result.msg,{icon:1,time:1000});
                          }else{
                              layer.msg(result.msg,{icon:2,time:1000});
                          }
                      },
                      error: function(data) {
                          layer.msg(data.msg,{icon:2,time:1000});
                      }

                  });
              });

              //全选
              form.on('checkbox(all)', function (data) {
                  $(".ids").prop("checked", data.elem.checked);
                  form.render('checkbox');
              });
              //有一个未选中全选取消选中
              form.on('checkbox(child)', function (data) {
                  var item = $(".ids");
                  for (var i = 0; i < item.length; i++) {
                      if (item[i].checked == false) {
                          $("#c_all").prop("checked", false);
                          form.render('checkbox');
                          break;
                      }
                  }
                  //如果都勾选了  勾上全选
                  var  all=item.length;
                  for (var i = 0; i < item.length; i++) {
                      if (item[i].checked == true) {
                          all--;
                      }
                  }
                  if(all==0){
                      $("#c_all").prop("checked", true);
                      form.render('checkbox');}
              });

          });

           /*用户-删除*/
          function member_del(obj,id){
              layer.confirm('确认要删除吗？',function(index){
                  //发异步删除数据
                  $.ajax({
                      type: "POST",
                      url: "{:url('brand_del')}",
                      data: {id:id},
                      dataType:'json',
                      success: function (result) {
                          if(result.code){
                              $(obj).parents("tr").remove();
                              layer.msg(result.msg,{icon:1,time:1000});
                          }else{
                              layer.msg(result.msg,{icon:2,time:1000});
                          }
                      },
                      error: function(data) {
                          layer.msg(data.msg,{icon:2,time:1000});
                      }

                  });


              });
          }


          function delAll() {
              var item=$("input[name='ids']:checked");
              var ids=[];
              $.each(item,function (vo,key) {
                  ids.push($(this).val())
              });

              if(ids.length<=0){
                  layer.alert("请选择要删除的项！", {
                      icon: 2
                  });
                  return false;
              }

              $.ajax({
                  type: "POST",
                  url: "{:url('brand_del')}",
                  data: {id:ids,type:1},
                  dataType:'json',
                  success: function (result) {
                      if(result.code){
                          layer.msg(result.msg, {
                              icon: 1,
                              time: 1000
                          },function () {
                              location.reload()
                          });
                      }else{
                          layer.msg(result.msg,{icon:2,time:1000});
                      }
                  },
                  error: function(data) {
                      layer.msg(data.msg,{icon:2,time:1000});
                  }

              });
          }
        </script>
    </body>
</html>
